#include "ndk/strace.h"
#include "ndk/logger.h"
#include "ndk/log_to_file.h"
#include "ndk/log_to_stdout.h"

namespace ndk
{

int logger::apply_module_config(module_entry *entry)
{
  STRACE("");
  if (entry == 0) return -1;
  if (this->module_entry_ != 0)
  {
    delete this->module_entry_;
  }
  this->module_entry_ = entry;
  return this->apply_module_config_i();
}
int logger::apply_module_config_i()
{
  STRACE("");
  this->output_list_ = this->log_manager_->get_output_list(this->module_entry_);
#if 0
  // 1. Ouput module
  if (NDK_BIT_ENABLED(this->module_entry_->output_, 
                      module_entry::om_files))
  {
    if (this->files_output_ == 0)
    {
      this->files_output_ = new log_to_file();
      this->files_output_->open(this->module_entry_);
    }else
    {
      this->files_output_->update_config(this->module_entry_);
    }
  }else
  {
    if (this->files_output_)
    {
      delete this->files_output_;
      this->files_output_ = 0;
    }
  }
  //  
  if (NDK_BIT_ENABLED(this->module_entry_->output_, 
                      module_entry::om_stdout))
  {
    if (this->stdout_output_ == 0)
    {
      this->stdout_output_ = new log_to_stdout();
      this->stdout_output_->open(this->module_entry_);
    }else
    {
      this->stdout_output_->update_config(this->module_entry_);
    }
  }else
  {
    if (this->stdout_output_)
    {
      delete this->stdout_output_;
      this->stdout_output_ = 0;
    }
  }
#endif
  return 0;
}
int logger::output(const char *record, const int len, int log_type, time_t now)
{
  STRACE("");
  if (this->output_list_.empty()) return 0;
  std::deque<log_output *>::iterator iter = this->output_list_.begin();
  int result = 0;
  for (; iter != this->output_list_.end(); ++iter)
  {
    result = (*iter)->log(record, len, log_type, now);
  }
  return result;
}

size_t mtrace::count_ = 0;
} // namespace ndk

